<template>
  <el-card style="max-width: 99%">
    <template #header>
      <div class="card-header">
        <span>管理员管理</span>
        <el-button type="primary" style="height: 36px;float: right" @click="openTc(1)">添加管理员</el-button>
      </div>
    </template>
    <el-row style="margin-bottom: 20px">
      <el-col :span="5">
        <el-form :model="form" style="max-width: 200px;margin-left: 25px">
          <el-form-item label="管理员">
            <el-input v-model="manager" placeholder="请输入管理员" />
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="19">
        <el-button type="primary" @click="cx()">搜索</el-button>
        <el-button @click="cz()">重置</el-button>
      </el-col>
    </el-row>
    <el-table :data="list" border style="margin-bottom: 15px">
      <el-table-column prop="manager" label="管理员" width="180" />
      <el-table-column prop="account" label="账户" width="180" />
      <el-table-column prop="sex" label="性别">
        <template #default="scope">
          <span v-if="scope.row.sex==1">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" />
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column prop="headUrl" label="头像">
        <template #default="scope">
          <el-image :src="scope.row.headUrl" style="width: 50px;height: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template #default="scope">
          <el-button :icon="Edit" circle @click="openTc(2,scope.row.id)"/>
          <el-button :icon="SemiSelect" circle  @click="open(scope.row.id)"/>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 0 auto;width: 300px">
      <el-pagination @current-change="handleCurrentChange"
                     size="small" background layout="prev, pager, next" :total="count" :page-size=pageSize class="mt-4"/>
    </div>
  </el-card>

  <el-dialog v-model="dialogFormVisible" :before-close="qx" :title="this.type==1?'添加管理员':'编辑信息'" width="500">
    <el-form :model="manageForm" style="margin-right: 60px;margin-left: 60px">
      <el-form-item label="管理员" prop="manager">
        <el-input v-model="manageForm.manager" autocomplete="off" />
      </el-form-item>
      <el-form-item label="账户" prop="account">
        <el-input v-model="manageForm.account" autocomplete="off" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="manageForm.sex" placeholder="请选择性别" style="width: 100%">
          <el-option label="男" value="1" />
          <el-option label="女" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="manageForm.phone" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="manageForm.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="头像图片">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8888/manage/upload-img"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
        >
          <img v-if="manageForm.headUrl" :src="manageForm.headUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="qx">取消</el-button>
        <el-button type="primary" @click="qd()">确定</el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script>
import {selectAllManage, selectManageById, deleteManage, addManage, updateManage} from "@/api/vue3";
import {ElMessage, ElMessageBox} from "element-plus";
import {Edit, SemiSelect} from "@element-plus/icons-vue";
export default {
  name: 'Manager',
  computed: {
    SemiSelect() {
      return SemiSelect
    },
    Edit() {
      return Edit
    }
  },
  data(){
    return{
      manager:'',//管理员信息
      pageNum:1,//当前页
      pageSize:5,//每页显示条数
      count:0,//总条数
      list:[],//管理员列表
      manageForm:{
        headUrl:''
      },//管理员表单
      dialogFormVisible:false,//弹窗
    }
  },
  mounted() {
    this.selList()
  },
  methods:{
    handleAvatarSuccess(file) {//上传成功
      console.log(file)
      this.manageForm.headUrl = file.data;
      console.log(this.manageForm.headUrl)
    },
    beforeAvatarUpload(file) {//上传之前
      const isJPG = file.type === 'image/jpeg' || file.type=='image/png';
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return isJPG && isLt2M;
    },
    selList(){
      selectAllManage(this.manager,this.pageNum,this.pageSize).then(res=>{
        this.list=res.data.data.list
        this.count=res.data.data.count
      })
    },
    handleCurrentChange(val) {//当前页码改变的事件
      this.pageNum = val;
      this.selList()
    },
    cx(){
      this.manager
      this.selList()
    },
    cz(){
      this.manager=''
      this.selList()
    },
    //打开弹窗
    openTc(type,id){
      this.type= type
      this.dialogFormVisible= true
      if (this.type==2){
        selectManageById(id).then(res=>{
          // console.log(res)
          this.manageForm=res.data.data.data
        })

      }
    },
    qd(){
      if(this.type==1){
        addManage(this.manageForm).then(res=>{
          console.log(res)
          if(res.data.data.code==200){
            this.selList()
            this.dialogFormVisible=false
            this.manageForm={}
            ElMessage({
              type: 'success',
              message: '添加已完成',
            })
          }else {
            this.dialogFormVisible=false
            ElMessage({
              type: 'error',
              message: '添加失败',
            })
          }
        })
      }
      if(this.type==2){
        updateManage(this.manageForm).then(res=> {
          if (res.data.data.code == 200) {
            this.selList()
            this.dialogFormVisible = false
            ElMessage({
              type: 'success',
              message: '修改已完成',
            })
            localStorage.setItem('headUrl', this.manageForm.headUrl)

            //强制刷新
            this.$router.go(0)
          } else {
            this.dialogFormVisible = false
            ElMessage({
              type: 'error',
              message: '修改失败',
            })

          }
        })
      }
    },
    qx(){
      this.manageForm={}
      this.dialogFormVisible=false
    },
    open(id) {//删除弹窗
      ElMessageBox.confirm(
          '是否删除该条信息',
          '消息提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      )
          .then(() => {
            deleteManage(id).then(res=>{
              if(res.data.data.code==200){
                this.selList()
                ElMessage({
                  type: 'success',
                  message: '删除已完成',
                })
              }else {
                ElMessage({
                  type: 'error',
                  message: '删除失败',
                })
              }
            })
          })
          .catch(() => {
            ElMessage({
              type: 'info',
              message: '删除已取消',
            })
          })
    },
  }
}
</script>

<style>
.el-card{
  width: 100%;
  height: 600px;
  background-color: white;
  margin-top: 20px;
  border-radius: 30px;
}
.el-table{
  margin: 0 auto;
  width: 96%;
}


.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>